'************************************************************************************
' Copyright  2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
'  Summary: Provides support for running XSLT templates
'  Refactor: There are too many translation methods

Option Strict On
Option Explicit On

Imports System
Imports System.Diagnostics
Imports CodeSmith.Engine


Namespace KADGen.CodeGenerationSupport

   Public Class CodeSmithSupport

      '#Region "Class level declarations - empty"
      '#End Region

      '#Region "Constructors - empty"
      '#End Region

#Region "Public Methods and Properties"
      'Public Shared Sub GenerateViaCodeSmith( _
      '            ByVal template As CodeTemplate, _
      '            ByVal MetaDataFileName As String, _
      '            ByVal outputFile As String, _
      '            ByVal outputType As OutputFileType)
      '   Dim xmlDoc As New Xml.XmlDocument
      '   xmlDoc.Load(MetaDataFileName)
      '   KADGen.LibraryInterop.Singletons.XMLDoc = xmlDoc
      '   KADGen.LibraryInterop.Singletons.NsMgr = KADGen.Utility.xmlHelpers.BuildNamespacesManagerForDoc(xmlDoc)
      '   GenerateViaCodeSmith(template, xmlDoc, outputFile, outputType)
      'End Sub


      Public Shared Function GenerateViaCodeSmith( _
                  ByVal template As CodeTemplate, _
                  ByVal metaData As MetadataWrapper.All, _
                  ByVal outputFile As String, _
                  ByVal outputType As OutputFileType) _
                  As IO.Stream
         Dim stream As IO.MemoryStream = Nothing
         Try
            stream = New IO.MemoryStream
            GenerateCodeSmithToStream(template, metaData, stream, outputType)
         Finally
            If stream IsNot Nothing Then
               stream.Flush()
               stream.Seek(0, IO.SeekOrigin.Begin)
            End If
         End Try
         Return stream

      End Function

#End Region

      '#Region "Protected and Friend Methods and Properties - empty"
      '#End Region

#Region "Private Methods and GenerateCodeToStream"
      Private Shared Sub GenerateCodeSmithToStream( _
                  ByVal template As CodeTemplate, _
                  ByVal metaData As MetadataWrapper.All, _
                  ByVal stream As IO.Stream, _
                  ByVal outputType As OutputFileType)

         Dim writer As IO.StreamWriter = Nothing
         Try
            writer = New IO.StreamWriter(stream)
            If template Is Nothing Then
               Throw New System.Exception("Template is nothing")
            End If
            template.Render(writer)
         Finally
            If writer IsNot Nothing Then
               writer.Flush()
            End If
         End Try



         'Dim xslt As New Xml.Xsl.XslCompiledTransform(True)
         'Dim streamWriter As New IO.StreamWriter(stream)
         'Dim args As New Xml.Xsl.XsltArgumentList

         'Try
         '   If xmlMetaData Is Nothing Then
         '      xmlMetaData = New Xml.XmlDocument
         '   End If

         '   xslt.Load(xsltFileName)
         '   GenerateXSLTToStream(xslt, xmlMetaData, stream, outputType, extObjects, params)
         'Finally
         '   If Not streamWriter Is Nothing Then
         '      streamWriter.Flush()
         '   End If
         'End Try
      End Sub

      'Private Shared Sub GenerateXSLTToStream( _
      '         ByVal xsltStream As IO.Stream, _
      '         ByVal xmlMetaData As Xml.XmlDocument, _
      '         ByVal stream As IO.Stream, _
      '            ByVal outputType As String, _
      '          ByVal extObjects() As ExtObject, _
      '         ByVal ParamArray params() As XSLTParam)
      '   Dim xslt As New Xml.Xsl.XslCompiledTransform(True)
      '   Dim xNav As Xml.XPath.XPathNavigator
      '   Dim streamWriter As New IO.StreamWriter(stream)
      '   Dim args As New Xml.Xsl.XsltArgumentList
      '   Dim xmlReader As Xml.XmlTextReader

      '   Try
      '      If xmlMetaData Is Nothing Then
      '         xmlMetaData = New Xml.XmlDocument
      '      End If

      '      xmlReader = New Xml.XmlTextReader(xsltStream)
      '      xslt.Load(xmlReader)
      '      GenerateXSLTToStream(xslt, xmlMetaData, stream, outputType, extObjects, params)
      '   Finally
      '      If Not streamWriter Is Nothing Then
      '         streamWriter.Flush()
      '      End If
      '   End Try
      'End Sub

      'Private Shared Sub GenerateXSLTToStream( _
      '            ByVal xsltTransform As Xml.Xsl.XslCompiledTransform, _
      '            ByVal xmlMetaData As Xml.XmlDocument, _
      '            ByVal stream As IO.Stream, _
      '            ByVal outputType As String, _
      '            ByVal extObjects() As ExtObject, _
      '            ByVal ParamArray params() As XSLTParam)
      '   Dim streamWriter As IO.StreamWriter = Nothing
      '   Dim xmlWriter As Xml.XmlTextWriter = Nothing
      '   Dim args As New Xml.Xsl.XsltArgumentList
      '   Dim param As XSLTParam

      '   Try
      '      If Not params Is Nothing Then
      '         For Each param In params
      '            args.AddParam(param.Name, "", param.Value)
      '         Next
      '      End If


      '      streamWriter = New IO.StreamWriter(stream)
      '      xsltTransform.Transform(New Xml.XmlNodeReader(xmlMetaData), args, streamWriter)
      '   Finally
      '      If Not streamWriter Is Nothing Then
      '         streamWriter.Flush()
      '      End If
      '      If Not xmlWriter Is Nothing Then
      '         xmlWriter.Flush()
      '      End If
      '   End Try
      'End Sub

      'Private Shared Sub GenerateXSLTToStream( _
      '            ByVal xsltFileName As String, _
      '            ByVal xmlMetaData As Xml.XmlDocument, _
      '            ByVal stream As IO.Stream, _
      '            ByVal outputType As String, _
      '            ByVal extObjects() As ExtObject, _
      '            ByVal ParamArray params() As XSLTParam)
      '   Dim xslt As New Xml.Xsl.XslCompiledTransform
      '   Dim xNav As Xml.XPath.XPathNavigator
      '   Dim streamWriter As New IO.StreamWriter(stream)
      '   Dim args As New Xml.Xsl.XsltArgumentList

      '   Try
      '      If xmlMetaData Is Nothing Then
      '         xmlMetaData = New Xml.XmlDocument
      '      End If

      '      xNav = xmlMetaData.CreateNavigator()

      '      xslt.Load(xsltFileName)
      '      GenerateXSLTToStream(xslt, xNav, stream, outputType, extObjects, params)
      '   Finally
      '      If Not streamWriter Is Nothing Then
      '         streamWriter.Flush()
      '      End If
      '   End Try
      'End Sub

      'Private Shared Sub GenerateXSLTToStream( _
      '         ByVal xsltStream As IO.Stream, _
      '         ByVal xmlMetaData As Xml.XmlDocument, _
      '         ByVal stream As IO.Stream, _
      '            ByVal outputType As String, _
      '          ByVal extObjects() As ExtObject, _
      '         ByVal ParamArray params() As XSLTParam)
      '   Dim xslt As New Xml.Xsl.XslCompiledTransform
      '   Dim xNav As Xml.XPath.XPathNavigator
      '   Dim streamWriter As New IO.StreamWriter(stream)
      '   Dim args As New Xml.Xsl.XsltArgumentList
      '   Dim xmlReader As Xml.XmlTextReader

      '   Try
      '      If xmlMetaData Is Nothing Then
      '         xmlMetaData = New Xml.XmlDocument
      '      End If

      '      xNav = xmlMetaData.CreateNavigator()

      '      xmlReader = New Xml.XmlTextReader(xsltStream)
      '      xslt.Load(xmlReader)
      '      GenerateXSLTToStream(xslt, xNav, stream, outputType, extObjects, params)
      '   Finally
      '      If Not streamWriter Is Nothing Then
      '         streamWriter.Flush()
      '      End If
      '   End Try
      'End Sub

      'Private Shared Sub GenerateXSLTToStream( _
      '            ByVal xsltTransform As Xml.Xsl.XslCompiledTransform, _
      '            ByVal xNavMetaData As Xml.XPath.XPathNavigator, _
      '            ByVal stream As IO.Stream, _
      '            ByVal outputType As String, _
      '            ByVal extObjects() As ExtObject, _
      '            ByVal ParamArray params() As XSLTParam)
      '   Dim streamWriter As IO.StreamWriter = Nothing
      '   Dim xmlWriter As Xml.XmlTextWriter = Nothing
      '   Dim args As New Xml.Xsl.XsltArgumentList
      '   Dim param As XSLTParam

      '   Try
      '      If Not params Is Nothing Then
      '         For Each param In params
      '            args.AddParam(param.Name, "", param.Value)
      '         Next
      '      End If

      '      If extObjects Is Nothing Then
      '         ' No problem, just skip
      '      Else
      '         For Each extObject As ExtObject In extObjects
      '            Dim constructorInfo As Reflection.ConstructorInfo = CType(extObject.Value, System.Type).GetConstructor(Type.EmptyTypes)
      '            Dim obj As Object = constructorInfo.Invoke(Nothing)
      '            args.AddExtensionObject(extObject.NameSpaceURI, obj)
      '         Next
      '      End If
      '      AddStandardExtension(args)

      '      streamWriter = New IO.StreamWriter(stream)
      '      xsltTransform.Transform(xNavMetaData, args, streamWriter)
      '   Finally
      '      If Not streamWriter Is Nothing Then
      '         streamWriter.Flush()
      '      End If
      '      If Not xmlWriter Is Nothing Then
      '         xmlWriter.Flush()
      '      End If
      '   End Try
      'End Sub


#End Region
   End Class

End Namespace